#include "common.fxh"

uniform const float EmIntensity = 0.6;

struct VertexInput
{
	float4 position		: POSITION0;
	float2 texCoord		: TEXCOORD0;
	float3 normal		: NORMAL0;
	float3 binormal     : BINORMAL0;
	float3 tangent		: TANGENT0;
};


struct VertexOutput
{
	float4 Position			: POSITION0;
	float2 texCoord			: TEXCOORD0;
	float Depth				: TEXCOORD1;
	float3x3 tangentToWorld	: TEXCOORD2;
};

VertexOutput VertexShader(VertexInput input )
{
	VertexOutput output;

	output.texCoord	= input.texCoord;
	output.Position	= mul(input.position,WorldViewProjection);


	output.Depth	= mul(input.position,WorldView).z;
	output.tangentToWorld[0]	= mul(input.tangent,WorldView);
	output.tangentToWorld[1]	= mul(input.binormal,WorldView);
	output.tangentToWorld[2]	= mul(input.normal,WorldView);
	return output;
}


PixelShaderOutput PixelShader(VertexOutput input,float face : VFACE)
{
	PixelShaderOutput output;

	float2 texCoord = input.texCoord * textureTiling;

	output.Color	= tex2D(DiffuseTextureSampler,texCoord);
	clip(output.Color.a - 0.8);
	output.Color.a	= 1;

	output.Normal.xyz	= normalize(((tex2D(NormalTextureSampler,texCoord).rgb)) * 2.0f - 1.0f);
	output.Normal.xyz	= mul(output.Normal.xyz,input.tangentToWorld);
	output.Normal.xyz	= normalize(output.Normal.xyz);

//	output.Normal.xyz	= face * normalize(input.tangentToWorld[2]);
//	output.Normal.xy	= float2(dot(normalFromMap,float3(1,0,0)),dot(float3(0,1,0),normalFromMap));
	output.Normal.xyz	= 0.5f * (output.Normal.xyz + 1.0f);

	output.Normal.w	= tex2D(SpecularTextureSampler,texCoord).r;

	output.Depth	= input.Depth;
	output.Back	= 1;
	return output;
}


technique Full
{
	pass GO
	{
		CullMode	= NONE;//CCW;
		VertexShader = compile vs_3_0 VertexShader();
		PixelShader = compile ps_3_0 PixelShader();
	}
}
